//
//  JXGovementViewController.m
//  JXAddressApp
//
//  Created by zhhw_home on 2019/1/6.
//  Copyright © 2019年 zhhw_home. All rights reserved.
//

#import "JXRegionViewController.h"
#import <QMUIKit/UIColor+QMUI.h>
#import "JXNewsTableViewCell.h"
#import "Masonry.h"
#import "EgovaNetworking.h"
#import "JXNewModel.h"
#import "UIImageView+AFNetworking.h"
#import "JXGovementModel.h"
#import "YYModel.h"
#import "RATreeView.h"
#import "JXBaseWKWebViewController.h"
#import "JXRegion2ViewController.h"

@interface JXRegionViewController ()
<UITableViewDelegate,
UITableViewDataSource>

@property (nonatomic, strong) UITableView        *tableView;
@property (nonatomic, strong) NSArray<JXGovementModel*>           *models;


@property (nonatomic, strong) NSArray<JXGovementModel*>           *disps;

@end

@implementation JXRegionViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initRootView];
    [self initdt];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}

- (void)initRootView
{
    self.view.backgroundColor = [UIColor qmui_colorWithHexString:@"#EAEAEA"];
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    
    self.tableView.tableFooterView = [UIView new];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}

- (void)initdt
{
    NSString *url = @"http://www.wegis.net/demo/home/baseinfo/getregionbyseniorid?seniorID=1";
    [EgovaNetworking getWithUrl:url cache:NO completed:^(NSInteger code, id data, NSString *msg) {
        NSLog(@"%@", data);
        if (data[@"treeList"]) {
            self.models = [NSArray yy_modelArrayWithClass:[JXGovementModel class] json:data[@"treeList"]];
        }
        
        [self.tableView reloadData];
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JXGovementModel *model = [self.models objectAtIndex:indexPath.row];
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

    cell.textLabel.text = model.text;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    JXGovementModel *model = [self.models objectAtIndex:indexPath.row];
    
    JXRegion2ViewController *vc = [JXRegion2ViewController new];
    vc.regionId = model.gid;
        
    vc.title = model.text;
    vc.hidesBottomBarWhenPushed = YES;
    [self.parentViewController.navigationController pushViewController:vc animated:YES];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
